import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;


public class PSPPacket {
	public static byte[] create_packet(PSPAction m_type, String client_ID, int packet_ID, String channel_name, PSPPubSchedule pub_sched, String message){
		byte[] data = new byte[Globals.packetSize];
		
		ArrayList packet_list = new ArrayList();
		
		// Check to see if action sent is valid
		try{
			m_type = (PSPAction.valueOf(m_type.toString()));
		} 
		catch(Exception e){ // If not a valid type
			m_type = null;
		}
		
		if (m_type != null)
			packet_list.add(m_type); // Add the PSPAction response to the packet
		else
			return null; // Return a null data to signify an invalid event type
		
		// For a datestamp, just put the current date and time
		try{
			packet_list.add(new Date());
		}
		catch(Exception e){
			return null;
		} 
		
		// Add the current Client ID
		if (client_ID != null)
			packet_list.add(client_ID);
		else 
			return null;
				
		// Add the current packet ID
		if (packet_ID >= 0)
			packet_list.add(packet_ID);
		else
			return null;
		
		// Add the current channel name
		if (channel_name != null)
			packet_list.add(channel_name);
		else
			return null;
		
		try{
			pub_sched = (PSPPubSchedule.valueOf(pub_sched.toString()));
		} 
		catch(Exception e){ // If not a valid type
			pub_sched= null;
		}
		
		if (pub_sched != null)
			packet_list.add(pub_sched); // Add the PSPAction response to the packet
		else
			return null; // Return a null data to signify an invalid event type
		
		// If the data represents a packet, then parse it appropriately.
		switch(PSPAction.valueOf(m_type.toString())){
			case SERVER_PUBLISH_EVENT:
			case SERVER_NOTIFY:
			   //String[] event_arr = new String[2];	
			break;
			default:
				break;
		}
		
		// Add the packet data
		if (message != null)
			packet_list.add(message);
		else
			return null;
		
		String packet_string = "";
		Iterator itr = packet_list.iterator();
		
		// Iterate through the packet and create data packet string
		while(itr.hasNext()){
			packet_string = packet_string + itr.next().toString() + Globals.data_separator;
		}							
		
		return packet_string.getBytes();
	}
	
	// Function that returns the proper response for the action provided
	private static PSPAction getResponse(PSPAction action){
		PSPAction response;
		
		switch(action){
		case CLIENT_SET_CHANNEL:
		case CLIENT_PUBLISH_EVENT:
		case CLIENT_SUBSCRIBE_CHANNEL:
			response = PSPAction.SERVER_RESPONSE_POS;
			break;
		case CLIENT_ACK:
			response = PSPAction.SERVER_NOTIFY;
			break;
		default:
			response = null;
			break;
		}
		
		return response;
	}
}
